package com.sx.zongzhi.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.compress.Luban;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.SpinnerPubAdapter;
import com.sx.zongzhi.bean.GlobalBean;
import com.sx.zongzhi.bean.MsgEvent;
import com.sx.zongzhi.bean.NewSocietyOrganizationBean;
import com.sx.zongzhi.bean.PhotoBean;
import com.sx.zongzhi.bean.PublicBean;
import com.sx.zongzhi.bean.UserBean;
import com.sx.zongzhi.net.ServiceFactory;
import com.sx.zongzhi.presenter.AddSocietyOrganizationPest;
import com.sx.zongzhi.presenter.PublicListPest;
import com.sx.zongzhi.presenter.UploadFilePest;
import com.sx.zongzhi.utils.BaseUtil;
import com.sx.zongzhi.utils.MyRxBus;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.CommentView;
import com.sx.zongzhi.view.IPublicBeanView;
import com.sx.zongzhi.view.UploadFileView;
import com.sx.zongzhi.widget.LoadDialog;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import xiaofei.library.datastorage.DataStorageFactory;
import xiaofei.library.datastorage.IDataStorage;

/**
 * 修改新社会
 * Created by admin on 2017/12/9.
 */

public class UpdateNewSocietyOrganizationActivity extends BaseActivity implements CommentView, IPublicBeanView, UploadFileView {


    @BindView(R.id.imv1)
    ImageView imv1;
    @BindView(R.id.imv_Head)
    ImageView imvHead;
    @BindView(R.id.rl_Head)
    RelativeLayout rlHead;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.sp_type)
    Spinner spType;
    @BindView(R.id.et_registrationNumbers)
    EditText etRegistrationNumbers;
    @BindView(R.id.et_registrationManagementCode)
    EditText etRegistrationManagementCode;
    @BindView(R.id.et_unifiedSocialCreditCode)
    EditText etUnifiedSocialCreditCode;
    @BindView(R.id.et_address)
    EditText etAddress;
    @BindView(R.id.et_residence)
    EditText etResidence;
    @BindView(R.id.sp_state)
    Spinner spState;
    @BindView(R.id.et_moneySource)
    EditText etMoneySource;
    @BindView(R.id.tv_approvalDate)
    TextView tvApprovalDate;
    @BindView(R.id.imv2)
    ImageView imv2;
    @BindView(R.id.sp_attentionExtent)
    Spinner spAttentionExtent;
    @BindView(R.id.et_legalPersonName)
    EditText etLegalPersonName;
    @BindView(R.id.et_legalSafePerson)
    EditText etLegalSafePerson;
    @BindView(R.id.et_legalSafePersonMobileNumber)
    EditText etLegalSafePersonMobileNumber;
    @BindView(R.id.et_legalSafePersonTelephone)
    EditText etLegalSafePersonTelephone;
    @BindView(R.id.et_legalPerson)
    EditText etLegalPerson;
    @BindView(R.id.sp_cardType)
    Spinner spCardType;
    @BindView(R.id.et_cardNo)
    EditText etCardNo;
    @BindView(R.id.et_legalPersonMobileNumber)
    EditText etLegalPersonMobileNumber;
    @BindView(R.id.et_legalPersonTelephone)
    EditText etLegalPersonTelephone;
    @BindView(R.id.rbt_Yes)
    RadioButton rbtYes;
    @BindView(R.id.rbt_No)
    RadioButton rbtNo;
    @BindView(R.id.rg_isHaveOverSea)
    RadioGroup rgIsHaveOverSea;
    @BindView(R.id.rbt_Yes1)
    RadioButton rbtYes1;
    @BindView(R.id.rbt_No1)
    RadioButton rbtNo1;
    @BindView(R.id.rg_isHaveOrganizationCondition)
    RadioGroup rgIsHaveOrganizationCondition;
    @BindView(R.id.rbt_Yes2)
    RadioButton rbtYes2;
    @BindView(R.id.rbt_No2)
    RadioButton rbtNo2;
    @BindView(R.id.rg_isHaveOrganization)
    RadioGroup rgIsHaveOrganization;
    @BindView(R.id.rbt_Yes3)
    RadioButton rbtYes3;
    @BindView(R.id.rbt_No3)
    RadioButton rbtNo3;
    @BindView(R.id.rg_isHaveGuild)
    RadioGroup rgIsHaveGuild;
    @BindView(R.id.rbt_Yes4)
    RadioButton rbtYes4;
    @BindView(R.id.rbt_No4)
    RadioButton rbtNo4;
    @BindView(R.id.rg_isHaveGroup)
    RadioGroup rgIsHaveGroup;
    @BindView(R.id.rbt_Yes5)
    RadioButton rbtYes5;
    @BindView(R.id.rbt_No5)
    RadioButton rbtNo5;
    @BindView(R.id.rg_isHaveWomenFederation)
    RadioGroup rgIsHaveWomenFederation;
    @BindView(R.id.et_partyMemberNumber)
    EditText etPartyMemberNumber;
    @BindView(R.id.et_theMemeberOfGuild)
    EditText etTheMemeberOfGuild;
    @BindView(R.id.et_theMemeberOfWomenFederation)
    EditText etTheMemeberOfWomenFederation;
    @BindView(R.id.et_theMemeberOfGroup)
    EditText etTheMemeberOfGroup;
    @BindView(R.id.ll_isHaveOrganization)
    RelativeLayout llIsHaveOrganization;
    @BindView(R.id.ll_isHaveWomenFederation)
    RelativeLayout llIsHaveWomenFederation;
    @BindView(R.id.ll_isHaveGroup)
    RelativeLayout llIsHaveGroup;
    @BindView(R.id.ll_isHaveGuild)
    RelativeLayout llIsHaveGuild;
    /**
     * 新经济组织类别 证件代码 社会关注程度 安全隐患类型
     */
    private PublicListPest publicListPest;
    private List<GlobalBean> typeList;
    private List<GlobalBean> chiefCardTypeList;
    private List<GlobalBean> attentionExtentList;
    private List<GlobalBean> stateList;

    private boolean isHaveOverSea = true,
            isHaveOrganizationCondition = true, isHaveOrganization = true,
            isHaveGuild = true, isHaveGroup = true, isHaveWomenFederation = true;

    private String id, orgId, address, residence,
            name, typeId, registrationNumbers,
            state, moneySource, approvalDate,
            attentionExtentId, legalPersonName,
            legalSafePerson, legalPerson, cardTypeId,
            cardNo, registrationManagementCode,
            unifiedSocialCreditCode, legalPersonMobileNumber,
            legalPersonTelephone, legalSafePersonMobileNumber,
            legalSafePersonTelephone,
            partyNum, theMemeberOfGuild,
            theMemeberOfWomenFederation, theMemeberOfGroup, filePath;

    private NewSocietyOrganizationBean bean;
    private boolean isCanLoad = true;

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_add_social);
        setTitleName("新社会组织修改");
        setTitleRight("确定");
        ButterKnife.bind(this);
        bean = (NewSocietyOrganizationBean) getIntent().getSerializableExtra("bean");
        id = bean.getId();
        initData();

        stateList = new ArrayList<>();
        GlobalBean bean = new GlobalBean();
        bean.setDisplayName("存在");
        bean.setId("1");
        GlobalBean bean1 = new GlobalBean();
        bean1.setDisplayName("不存在");
        bean1.setId("0");
        stateList.add(bean);
        stateList.add(bean1);
        SpinnerPubAdapter adapter = new SpinnerPubAdapter(this, R.layout.item_text, stateList);
        spState.setAdapter(adapter);
        showState();

        publicListPest = new PublicListPest(this);
        publicListPest.getPublicList("社会组织类型");
        publicListPest.getPublicList("社会关注程度");
        publicListPest.getPublicList("证件代码");

        IDataStorage dataStorage = DataStorageFactory.getInstance(
                this, DataStorageFactory.TYPE_DATABASE);
        if (dataStorage.contains(UserBean.class, getPackageName())) {
            UserBean userBean = dataStorage.load(UserBean.class, getPackageName());
            if (userBean != null) {
                orgId = userBean.getOrg().getId();
            }
        }
        initRadioBtn();
    }

    private void initData() {
        RequestOptions options = new RequestOptions().centerCrop()
                .placeholder(R.mipmap.img_deful_house)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .error(R.mipmap.img_deful_house);
        Glide.with(this)
                .load(ServiceFactory.NEW_API_BASE_URL+ File.separator + bean.getImgUrl())
                .apply(options)
                .into(imvHead);
        etName.setText(BaseUtil.isEmpty(bean.getName()) ? "" : bean.getName());
        etResidence.setText(BaseUtil.isEmpty(bean.getResidence()) ? "" : bean.getResidence());
        etRegistrationNumbers.setText(BaseUtil.isEmpty(bean.getRegistrationNumbers()) ? "" : bean.getRegistrationNumbers());
        etRegistrationManagementCode.setText(BaseUtil.isEmpty(bean.getRegistrationManagementCode()) ? "" : bean.getRegistrationManagementCode());
        etCardNo.setText(BaseUtil.isEmpty(bean.getCardNo()) ? "" : bean.getCardNo());
        etUnifiedSocialCreditCode.setText(BaseUtil.isEmpty(bean.getUnifiedSocialCreditCode()) ? "" : bean.getUnifiedSocialCreditCode());
        etAddress.setText(BaseUtil.isEmpty(bean.getAddress()) ? "" : bean.getAddress());
        etMoneySource.setText(BaseUtil.isEmpty(bean.getMoneySource()) ? "" : bean.getMoneySource());
        tvApprovalDate.setText(BaseUtil.isEmpty(bean.getApprovalDate()) ? "" : bean.getApprovalDate());
        etLegalPersonName.setText(BaseUtil.isEmpty(bean.getLegalPersonName()) ? "" : bean.getLegalPersonName());
        etLegalSafePerson.setText(BaseUtil.isEmpty(bean.getLegalSafePerson()) ? "" : bean.getLegalSafePerson());
        etLegalSafePersonMobileNumber.setText(BaseUtil.isEmpty(bean.getLegalSafePersonMobileNumber()) ? "" : bean.getLegalSafePersonMobileNumber());
        etLegalSafePersonTelephone.setText(BaseUtil.isEmpty(bean.getLegalSafePersonTelephone()) ? "" : bean.getLegalSafePersonTelephone());
        etLegalPerson.setText(BaseUtil.isEmpty(bean.getLegalPerson()) ? "" : bean.getLegalPerson());
        etLegalPersonMobileNumber.setText(BaseUtil.isEmpty(bean.getLegalPersonMobileNumber()) ? "" : bean.getLegalPersonMobileNumber());
        etLegalPersonTelephone.setText(BaseUtil.isEmpty(bean.getLegalPersonTelephone()) ? "" : bean.getLegalPersonTelephone());
        isHaveOverSea = bean.isDangerousEnterprise();
        if (isHaveOverSea) {
            rgIsHaveOverSea.check(R.id.rbt_Yes);
        } else {
            rgIsHaveOverSea.check(R.id.rbt_No);
        }
        isHaveOrganizationCondition = bean.isHaveOrganizationCondition();
        if (isHaveOrganizationCondition) {
            rgIsHaveOrganizationCondition.check(R.id.rbt_Yes1);
        } else {
            rgIsHaveOrganizationCondition.check(R.id.rbt_No1);
        }

        isHaveOrganization = bean.isHaveOrganization();
        if (isHaveOrganization){
            llIsHaveOrganization.setVisibility(View.VISIBLE);
            rgIsHaveOrganization.check(R.id.rbt_Yes2);
        }else {
            rgIsHaveOrganization.check(R.id.rbt_No2);
            llIsHaveOrganization.setVisibility(View.GONE);
        }

        isHaveGuild = bean.isHaveGuild();
        if (isHaveGuild){
            llIsHaveGuild.setVisibility(View.VISIBLE);
            rgIsHaveGuild.check(R.id.rbt_Yes3);
        }else {
            llIsHaveGuild.setVisibility(View.GONE);
            rgIsHaveGuild.check(R.id.rbt_No3);
        }

        isHaveGroup = bean.isHaveGroup();
        if (isHaveGroup){
            llIsHaveGroup.setVisibility(View.VISIBLE);
            rgIsHaveGroup.check(R.id.rbt_Yes4);
        }else {
            llIsHaveGroup.setVisibility(View.GONE);
            rgIsHaveGroup.check(R.id.rbt_No4);
        }

        isHaveWomenFederation = bean.isHaveWomenFederation();
        if (isHaveWomenFederation){
            llIsHaveWomenFederation.setVisibility(View.VISIBLE);
            rgIsHaveWomenFederation.check(R.id.rbt_Yes5);
        }else {
            llIsHaveWomenFederation.setVisibility(View.GONE);
            rgIsHaveWomenFederation.check(R.id.rbt_No5);
        }

        etPartyMemberNumber.setText(bean.getPartyNum() + "");
        etTheMemeberOfGuild.setText(bean.getTheMemeberOfGuild() + "");
        etTheMemeberOfWomenFederation.setText(bean.getTheMemeberOfWomenFederation() + "");
        etTheMemeberOfGroup.setText(bean.getTheMemeberOfGroup() + "");

    }

    private void initRadioBtn() {
        rgIsHaveOverSea.setOnCheckedChangeListener(new OnCheckedChangeListeners());
        rgIsHaveOrganizationCondition.setOnCheckedChangeListener(new OnCheckedChangeListeners());
        rgIsHaveOrganization.setOnCheckedChangeListener(new OnCheckedChangeListeners());
        rgIsHaveGuild.setOnCheckedChangeListener(new OnCheckedChangeListeners());
        rgIsHaveGroup.setOnCheckedChangeListener(new OnCheckedChangeListeners());
        rgIsHaveWomenFederation.setOnCheckedChangeListener(new OnCheckedChangeListeners());
    }

    class OnCheckedChangeListeners implements RadioGroup.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
            switch (group.getId()) {
                case R.id.rg_isHaveOverSea:
                    if (checkedId == R.id.rbt_Yes) {
                        isHaveOverSea = true;
                    } else {
                        isHaveOverSea = false;
                    }
                    break;
                case R.id.rg_isHaveOrganizationCondition:
                    if (checkedId == R.id.rbt_Yes1) {
                        isHaveOrganizationCondition = true;
                    } else {
                        isHaveOrganizationCondition = false;
                    }
                    break;
                case R.id.rg_isHaveOrganization:
                    if (checkedId == R.id.rbt_Yes2) {
                        isHaveOrganization = true;
                        llIsHaveOrganization.setVisibility(View.VISIBLE);
                        etPartyMemberNumber.setText("1");
                    } else {
                        isHaveOrganization = false;
                        llIsHaveOrganization.setVisibility(View.GONE);
                        etPartyMemberNumber.setText("0");
                    }
                    break;
                case R.id.rg_isHaveGuild:
                    if (checkedId == R.id.rbt_Yes3) {
                        isHaveGuild = true;
                        llIsHaveGuild.setVisibility(View.VISIBLE);
                        etTheMemeberOfGuild.setText("1");
                    } else {
                        isHaveGuild = false;
                        llIsHaveGuild.setVisibility(View.GONE);
                        etTheMemeberOfGuild.setText("0");
                    }
                    break;
                case R.id.rg_isHaveGroup:
                    if (checkedId == R.id.rbt_Yes4) {
                        isHaveGroup = true;
                        llIsHaveGroup.setVisibility(View.VISIBLE);
                        etTheMemeberOfGroup.setText("1");
                    } else {
                        isHaveGroup = false;
                        llIsHaveGroup.setVisibility(View.GONE);
                        etTheMemeberOfGroup.setText("0");
                    }
                    break;
                case R.id.rg_isHaveWomenFederation:
                    if (checkedId == R.id.rbt_Yes5) {
                        isHaveWomenFederation = true;
                        llIsHaveWomenFederation.setVisibility(View.VISIBLE);
                        etTheMemeberOfWomenFederation.setText("1");
                    } else {
                        isHaveWomenFederation = false;
                        llIsHaveWomenFederation.setVisibility(View.GONE);
                        etTheMemeberOfWomenFederation.setText("0");
                    }
                    break;
            }
        }
    }

    @OnClick({R.id.tv_right, R.id.imv_Head, R.id.tv_approvalDate})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.tv_right:
                isCanLoad = true;
                subMit();
                if (isCanLoad){
                    LoadDialog.show(UpdateNewSocietyOrganizationActivity.this, "提交中...");
                    if (!BaseUtil.isEmpty(filePath)) {
                        List<File> files = new ArrayList<>();
                        files.add(new File(filePath));
                        new UploadFilePest(this).uploadFile(this, files);
                    } else {
                        commitData("");
                    }
                }
                break;
            case R.id.imv_Head:
                PictureSelector.create(this)
                        .openGallery(PictureMimeType.ofImage())
                        .selectionMode(PictureConfig.SINGLE)
                        .maxSelectNum(1)
                        .previewImage(true)// 是否可预览图片
                        .enableCrop(true)
                        .compress(true)
                        .compressGrade(Luban.CUSTOM_GEAR)
                        .compressMaxKB(200)//压缩最大值kb compressGrade()为Luban.CUSTOM_GEAR有效 int
                        .forResult(PictureConfig.CHOOSE_REQUEST);
                break;
            case R.id.tv_approvalDate:
                BaseUtil.createDataPop(this, tvApprovalDate);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == PictureConfig.CHOOSE_REQUEST) {
                List<LocalMedia> picUrls = PictureSelector.obtainMultipleResult(data);
                if (!picUrls.isEmpty()) {
                    for (LocalMedia localMedia : picUrls) {
                        filePath = localMedia.getCompressPath();
                        RequestOptions options = new RequestOptions().centerCrop()
                                .placeholder(R.drawable.shape_ff9f9f_12bgd)
                                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                                .error(R.drawable.shape_ff9f9f_12bgd);
                        Glide.with(this)
                                .load(filePath)
                                .apply(options)
                                .into(imvHead);
                    }
                }
            }
        }
    }

    private void subMit() {
        name = etName.getText().toString().trim();
        if (BaseUtil.isEmpty(name)){
            etName.setError("请输入社会组织名称");
            isCanLoad = false;
            return;
        }
        if (name.length() > 50){
            etName.setError("长度不能大于50个字符");
            isCanLoad = false;
            return;
        }

        typeId = ((GlobalBean) spType.getSelectedItem()).getId();
        if (BaseUtil.isEmpty(typeId)) {
            ToastUtils.showToast(UpdateNewSocietyOrganizationActivity.this, "请选择社会组织名称类型");
            isCanLoad = false;
            return;
        }

        registrationNumbers = etRegistrationNumbers.getText().toString().trim();
        if (BaseUtil.isEmpty(registrationNumbers)){
            etRegistrationNumbers.setError("请输入登记证号");
            isCanLoad = false;
            return;
        }
        if (registrationNumbers.length() > 32){
            etRegistrationNumbers.setError("长度不能大于32个字符");
            isCanLoad = false;
            return;
        }

        registrationManagementCode = etRegistrationManagementCode.getText().toString().trim();
        if (BaseUtil.isEmpty(registrationManagementCode)){
            etRegistrationManagementCode.setError("请输入登记管理机关代码");
            isCanLoad = false;
            return;
        }
        if (registrationManagementCode.length() > 6) {
            etRegistrationManagementCode.setError("长度不能大于6个字符");
            isCanLoad = false;
            return;
        }

        unifiedSocialCreditCode = etUnifiedSocialCreditCode.getText().toString().trim();
        if (BaseUtil.isEmpty(unifiedSocialCreditCode)){
            etUnifiedSocialCreditCode.setError("请输入统一社会信用代码");
            isCanLoad = false;
            return;
        }
        if (unifiedSocialCreditCode.length() > 18) {
            etUnifiedSocialCreditCode.setError("长度不能大于18个字符");
            isCanLoad = false;
            return;
        }

        address = etAddress.getText().toString().trim();
        if (BaseUtil.isEmpty(address)){
            etAddress.setError("请输入办公地址");
            isCanLoad = false;
            return;
        }
        if (address.length() > 30) {
            etAddress.setError("长度不能大于30个字符");
            isCanLoad = false;
            return;
        }

        residence = etResidence.getText().toString().trim();
        if (BaseUtil.isEmpty(residence)){
            etResidence.setError("请输入住所");
            isCanLoad = false;
            return;
        }
        if (residence.length() > 50) {
            etResidence.setError("长度不能大于50个字符");
            isCanLoad = false;
            return;
        }

        state = ((GlobalBean) spState.getSelectedItem()).getId();
        if (BaseUtil.isEmpty(state)) {
            ToastUtils.showToast(UpdateNewSocietyOrganizationActivity.this, "请选择状态");
            isCanLoad = false;
            return;
        }

        moneySource = etMoneySource.getText().toString().trim();
        if (moneySource.length() > 30) {
            etMoneySource.setError("长度不能大于30个字符");
            isCanLoad = false;
            return;
        }

        approvalDate = tvApprovalDate.getText().toString().trim();
        if (BaseUtil.isEmpty(approvalDate)){
            ToastUtils.showToast(UpdateNewSocietyOrganizationActivity.this, "请选择批准日期");
            isCanLoad = false;
            return;
        }

        attentionExtentId = ((GlobalBean) spAttentionExtent.getSelectedItem()).getId();
        if (BaseUtil.isEmpty(attentionExtentId)) {
            ToastUtils.showToast(UpdateNewSocietyOrganizationActivity.this, "请选择关注程度");
            isCanLoad = false;
            return;
        }

        legalPersonName = etLegalPersonName.getText().toString().trim();
        if (BaseUtil.isEmpty(legalPersonName)){
            etLegalPersonName.setError("请输入法定代表人姓名");
            isCanLoad = false;
            return;
        }
        if (legalPersonName.length() > 20 || legalPersonName.length() < 2) {
            etLegalPersonName.setError("长度不能小于2个或大于20个字符");
            isCanLoad = false;
            return;
        }

        legalSafePerson = etLegalSafePerson.getText().toString().trim();
        if (BaseUtil.isEmpty(legalSafePerson)){
            etLegalSafePerson.setError("请输入治保负责人姓名");
            isCanLoad = false;
            return;
        }
        if (legalSafePerson.length() > 20 || legalSafePerson.length() < 2) {
            etLegalSafePerson.setError("长度不能小于2个或大于20个字符");
            isCanLoad = false;
            return;
        }

        legalSafePersonMobileNumber = etLegalSafePersonMobileNumber.getText().toString();
        legalSafePersonTelephone = etLegalSafePersonTelephone.getText().toString();
        if (BaseUtil.isEmpty(legalSafePersonMobileNumber) && BaseUtil.isEmpty(legalSafePersonTelephone)) {
            ToastUtils.showToast(UpdateNewSocietyOrganizationActivity.this, "治保负责人联系方式");
            isCanLoad = false;
            return;
        }
        if (!BaseUtil.isMobile(legalSafePersonMobileNumber)){
            etLegalSafePersonMobileNumber.setError("请输入正确手机号格式");
            isCanLoad = false;
            return;
        }

        legalPerson = etLegalPerson.getText().toString().trim();
        if (BaseUtil.isEmpty(legalPerson)){
            etLegalPerson.setError("请输入负责人姓名");
            isCanLoad = false;
            return;
        }
        if (legalPerson.length() > 20 || legalPerson.length() < 2) {
            etLegalPerson.setError("长度不能小于2个或大于20个字符");
            isCanLoad = false;
            return;
        }

        cardTypeId = ((GlobalBean) spCardType.getSelectedItem()).getId();
        if (BaseUtil.isEmpty(cardTypeId)) {
            ToastUtils.showToast(UpdateNewSocietyOrganizationActivity.this, "请选择负责人证件代码");
            isCanLoad = false;
            return;
        }
        cardNo = etCardNo.getText().toString().trim();
        if (BaseUtil.isEmpty(cardNo)){
            etCardNo.setError("请输入负责人证件号码");
            isCanLoad = false;
            return;
        }
        if (cardNo.length() > 50) {
            etCardNo.setError("长度不能大于50个字符");
            isCanLoad = false;
            return;
        }

        legalPersonMobileNumber = etLegalPersonMobileNumber.getText().toString();
        legalPersonTelephone = etLegalPersonTelephone.getText().toString();
        if (BaseUtil.isEmpty(legalPersonMobileNumber) && BaseUtil.isEmpty(legalPersonTelephone)) {
            ToastUtils.showToast(UpdateNewSocietyOrganizationActivity.this, "请完善带*号信息");
            isCanLoad = false;
            return;
        }
        if (!BaseUtil.isMobile(legalPersonMobileNumber)){
            etLegalPersonMobileNumber.setError("请输入正确手机号格式");
            isCanLoad = false;
            return;
        }

        partyNum = etPartyMemberNumber.getText().toString().trim();
        if (BaseUtil.isEmpty(partyNum)){
            etPartyMemberNumber.setError("请输入中共党员数量");
            isCanLoad = false;
            return;
        }
        if (Integer.parseInt(partyNum) < 0 || Integer.parseInt(partyNum) > 999999) {
            etPartyMemberNumber.setError("只能输入0~999999");
            isCanLoad = false;
            return;
        }

        theMemeberOfGuild = etTheMemeberOfGuild.getText().toString().trim();
        if (BaseUtil.isEmpty(theMemeberOfGuild)){
            etTheMemeberOfGuild.setError("请输入工会会员数量");
            isCanLoad = false;
            return;
        }
        if (Integer.parseInt(theMemeberOfGuild) < 0 || Integer.parseInt(theMemeberOfGuild) > 999999) {
            etTheMemeberOfGuild.setError("只能输入0~999999");
            isCanLoad = false;
            return;
        }

        theMemeberOfWomenFederation = etTheMemeberOfWomenFederation.getText().toString().trim();
        if (BaseUtil.isEmpty(theMemeberOfWomenFederation)){
            etTheMemeberOfWomenFederation.setError("请输入妇女数量");
            isCanLoad = false;
            return;
        }
        if (Integer.parseInt(theMemeberOfWomenFederation) < 0 || Integer.parseInt(theMemeberOfWomenFederation) > 999999) {
            etTheMemeberOfWomenFederation.setError("只能输入0~999999");
            isCanLoad = false;
            return;
        }

        theMemeberOfGroup = etTheMemeberOfGroup.getText().toString().trim();
        if (BaseUtil.isEmpty(theMemeberOfGroup)){
            etTheMemeberOfGroup.setError("请输入共青团员数量");
            isCanLoad = false;
            return;
        }
        if (Integer.parseInt(theMemeberOfGroup) < 0 || Integer.parseInt(theMemeberOfGroup) > 999999) {
            etTheMemeberOfGroup.setError("只能输入0~999999");
            isCanLoad = false;
            return;
        }
    }

    @Override
    public void uploadSuccess(List<PhotoBean> beans) {
        commitData(beans.get(0).getPath());
    }

    private void commitData(String urlPath) {
        new AddSocietyOrganizationPest(this).updateNewSocietyOrganization(this, id, orgId, address, residence,
                name, typeId, registrationNumbers,
                state, moneySource, approvalDate,
                attentionExtentId, legalPersonName,
                legalSafePerson, legalPerson, cardTypeId,
                cardNo, registrationManagementCode,
                unifiedSocialCreditCode, legalPersonMobileNumber,
                legalPersonTelephone, legalSafePersonMobileNumber,
                legalSafePersonTelephone,
                partyNum, theMemeberOfGuild,
                theMemeberOfWomenFederation, theMemeberOfGroup, urlPath, isHaveOverSea,
                isHaveOrganizationCondition, isHaveOrganization,
                isHaveGuild, isHaveGroup, isHaveWomenFederation,
                0, 0);
    }

    @Override
    public void commentSuccess(Boolean result) {
        LoadDialog.dismiss(UpdateNewSocietyOrganizationActivity.this);
        MyRxBus.get().post(new MsgEvent("NewSocietyDetailActivity", 1, "修改成功"));
        MyRxBus.get().post(new MsgEvent("NewSocietyOrganizationActivity", 1, "修改成功"));
        ToastUtils.showToast(UpdateNewSocietyOrganizationActivity.this, "修改成功");
        finish();
    }

    @Override
    public void showError(String message) {
        LoadDialog.dismiss(UpdateNewSocietyOrganizationActivity.this);
        ToastUtils.showToast(UpdateNewSocietyOrganizationActivity.this, message);
    }

    @Override
    public void getPublicList(PublicBean publicBean) {
        if ("社会组织类型".equals(publicBean.getDomainName())) {
            typeList = publicBean.getDicts();
            if (typeList != null) {
                SpinnerPubAdapter adapter = new SpinnerPubAdapter(this, R.layout.item_text, typeList);
                spType.setAdapter(adapter);
                if (bean.getType() != null) {
                    showType();
                }
            }
        }
        if ("证件代码".equals(publicBean.getDomainName())) {
            chiefCardTypeList = publicBean.getDicts();
            if (chiefCardTypeList != null) {
                SpinnerPubAdapter adapter = new SpinnerPubAdapter(this, R.layout.item_text, chiefCardTypeList);
                spCardType.setAdapter(adapter);
                if (bean.getCardType() != null) {
                    showCardType();
                }
            }
        }
        if ("社会关注程度".equals(publicBean.getDomainName())) {
            attentionExtentList = publicBean.getDicts();
            if (attentionExtentList != null) {
                SpinnerPubAdapter adapter = new SpinnerPubAdapter(this, R.layout.item_text, attentionExtentList);
                spAttentionExtent.setAdapter(adapter);
                if (bean.getAttentionExtent() != null) {
                    showAttentionExtent();
                }
            }
        }
    }

    private void showType() {
        for (int i = 0; i < typeList.size(); i++) {
            if (typeList.get(i).getId().equals(bean.getType().getId())) {
                spType.setSelection(i);
            }
        }
    }

    private void showCardType() {
        for (int i = 0; i < chiefCardTypeList.size(); i++) {
            if (chiefCardTypeList.get(i).getId().equals(bean.getCardType().getId())) {
                spCardType.setSelection(i);
            }
        }
    }

    private void showAttentionExtent() {
        for (int i = 0; i < attentionExtentList.size(); i++) {
            if (attentionExtentList.get(i).getId().equals(bean.getAttentionExtent().getId())) {
                spAttentionExtent.setSelection(i);
            }
        }
    }

    private void showState() {
        for (int i = 0; i < stateList.size(); i++) {
            if (stateList.get(i).getId().equals(bean.getState() + "")) {
                spAttentionExtent.setSelection(i);
            }
        }
    }
}
